package train_array.day2;

import java.util.*;

public class code5 {
    public static StringJoiner sj = new StringJoiner(",");
    public static ArrayList<ArrayList<Integer>> list = new ArrayList<>();

    public static void fun(int m){
        int len = list.size();
        LinkedList<Integer> l = new LinkedList<>();
        for(int i=0;i<len;i++){
            ArrayList<Integer> tep = list.get(i);
            int j=0;
            while ( j<m && !tep.isEmpty()){
                j +=1;
                sj.add(String.valueOf(tep.remove(0)));
            }
            if(tep.isEmpty()){
                l.addFirst(i);
            }
        }

        if(!l.isEmpty()){
            for(Integer i:l){
                int x = i;
                list.remove(x);
            }
        }

    }



    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = Integer.parseInt(sc.nextLine());
        int k = Integer.parseInt(sc.nextLine());

        for(int i=0;i<k;i++){
            ArrayList<Integer> l = new ArrayList<>();
            String[] str = sc.nextLine().split(",");
            for(String s:str){
                l.add(Integer.parseInt(s));
            }
            list.add(l);
        }

        System.out.println("----");

        while (!list.isEmpty()){
            fun(m);
        }

        System.out.println(list.size());
        System.out.println(sj.toString());

    }
}
